package net.m510.autosurfer;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Logger;

import net.m510.autosurfer.common.DocumentGetProcess;
import net.m510.autosurfer.common.cli.OutFileOption;
import net.m510.autosurfer.common.cli.PasswordOption;
import net.m510.autosurfer.core.AbstractMain;
import net.m510.autosurfer.core.exception.UserProcessException;
import net.m510.autosurfer.sonybank.SonyBankAnnouncement;
import net.m510.autosurfer.sonybank.SonyBankCheckLastDeal;
import net.m510.autosurfer.sonybank.SonyBankDownload;
import net.m510.autosurfer.sonybank.SonyBankGoAccountTop;
import net.m510.autosurfer.sonybank.SonyBankGoDealHistory;
import net.m510.autosurfer.sonybank.SonyBankLogin;
import net.m510.autosurfer.sonybank.SonyBankLoginChecker;
import net.m510.autosurfer.sonybank.SonyBankLogout;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.jsoup.nodes.Document;

public class SonyBankGetMain extends AbstractMain {
	private static final Logger log = Logger.getLogger(SonyBankGetMain.class.getName());
	private static final String DEFAULT_MEISAI_FILENAME="YenFutsuRireki.csv";
	
	private String tenNo;
	private String kozaNo;
	private String password;
	private File outFile;
	
	private String cid;
	private String tid;
	private String zid;

	/**
	 * 
	 */
	public SonyBankGetMain() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SonyBankGetMain main = new SonyBankGetMain();
		main.setLogger(log);
		main.Main(args);
	}
	@Override
	protected Document start() throws IOException, UserProcessException {
		DocumentGetProcess p = new DocumentGetProcess(client,context) {
			
			@Override
			protected URI getUri(Document doc) {
				return URI.create("https://o2o.moneykit.net/NBG100001G01.html");
			}
		};
		Document out = p.process(null);
		return out;
	}

	@Override
	protected Document login(Document doc) throws IOException, UserProcessException {
		SonyBankLogin p = new SonyBankLogin(client, context);
		p.setTenNo(tenNo);
		p.setKozaNo(kozaNo);
		p.setPassword(password);
		p.setTid(tid);
		p.setCid(cid);
		p.setZid(zid);
		Document out = p.process(doc);
		new SonyBankLoginChecker().check(out);
		return out;
	}

	@Override
	protected Document prepareDownload(Document doc) throws IOException,UserProcessException {
		log.info("前回取引確認処理開始");
		Document out1 = new SonyBankCheckLastDeal(client, context).process(doc);
		log.info("前回取引確認処理終了 ");
		
		log.info("お知らせ処理開始");
		Document out2 = new SonyBankAnnouncement(client, context).process(out1);
		log.info("お知らせ処理終了 ");

		log.info("口座トップページへの転送処理開始");
		Document out3 = new SonyBankGoAccountTop(client, context).process(out2);
		log.info("口座トップページへの転送処理終了 ");

		log.info("取引履歴ページへの遷移開始");
		Document out4 = new SonyBankGoDealHistory(client, context).process(out3);
		log.info("取引履歴ページへの遷移終了");
		return out4;
	}

	@Override
	protected File download(Document doc) throws IOException, UserProcessException {
		SonyBankDownload p = new SonyBankDownload(client, context, outFile);
		File out = p.process(doc);
		return out;
	}

	@Override
	protected void logout(Document doc) throws IOException,	UserProcessException {
		new SonyBankLogout(client, context).process(doc);
	}
	
	@SuppressWarnings("static-access")
	@Override
	protected Options getOptions() {
		Options options = new Options();
		options.addOption(new OutFileOption(false));
		options.addOption(OptionBuilder.hasArg(true).withArgName("nnn")
				.isRequired(true).withDescription("店番号")
				.create("ten"));
		options.addOption(OptionBuilder.hasArg(true).withArgName("account no")
				.isRequired(true).withDescription("口座番号")
				.create("account"));
		options.addOption(new PasswordOption(true));
		options.addOption(OptionBuilder.hasArg(true).withArgName("9999999999")
				.isRequired(true).withDescription("CookieId")
				.create("cid"));
		options.addOption(OptionBuilder.hasArg(true).withArgName("9")
				.isRequired(true).withDescription("TanmatsuShubt")
				.create("t"));
		options.addOption(OptionBuilder.hasArg(true).withArgName("yyyy/mm/dd hh:mm:ss")
				.isRequired(true).withDescription("ZenkaiLogonTime")
				.create("z"));
		return options;
	}

	@Override
	protected void setParams(CommandLine cmd) {
		tenNo = cmd.getOptionValue("ten");
		kozaNo = cmd.getOptionValue("account");
		password = cmd.getOptionValue("password");
		outFile = new File(cmd.getOptionValue("out", DEFAULT_MEISAI_FILENAME));
		cid = cmd.getOptionValue("cid");
		tid = cmd.getOptionValue("t");
		zid = cmd.getOptionValue("z");
	}

}
